package com.hzit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzit.entity.Classes;
import com.hzit.entity.PageResult;
import com.hzit.entity.Student;
import com.hzit.mapper.StudentMapper;
import com.hzit.service.IClassesService;
import com.hzit.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hbxfwf
 * @since 2023-05-25
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
	@Autowired
	private IClassesService classesService;
	//1. 分页查询学生
	@Override
	public PageResult<Student> search(int page, int pageSize, Student student) {
		//1.1 构造查询条件
		QueryWrapper<Student> wrapper = new QueryWrapper<>();
		if(student != null){
			wrapper.like(StringUtils.isNotBlank(student.getSname()),"sname",student.getSname())
					.like(StringUtils.isNotBlank(student.getAddr()),"addr",student.getAddr())
					.eq(student.getCid() != null && student.getCid() != 0,"cid",student.getCid());
		}
		//1.2 开始分页
		Page<Student> studPage = this.page(new Page(page, pageSize), wrapper);
		//1.3 转化为pageResult对象
		List<Student> records = studPage.getRecords();
		for (Student st : records) {
			Classes classes = classesService.getById(st.getCid());
			st.setCname(classes.getCname());
		}
		return new PageResult<>(studPage.getTotal(),records);
	}
}
